using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using OpenComposite.Base.Designer;

namespace OpenComposite.EII.Designer.SCA
{
	class InterfaceConnectorSymbol : Connector, IRepositoryItemSymbol
	{
		#region Initialization
		public InterfaceConnectorSymbol()
		{
			init();
		}
		private void init()
		{
			this.BeginUpdate();

			_folder = new OpenComposite.EII.Repository.GenericFolder(true);

			//this.Name = "interfaceConnectorSymbol" + GetCount(typeof(InterfaceConnectorSymbol));
			this.Text = "Service";

			this.NameTextBox.Visible = false;

			this.MinimumSize = new Size(25, 15);
			this.Size = new System.Drawing.Size(25, 15);
			this.Font = new Font(FontFamily.GenericSerif, 12, FontStyle.Bold);

			this.Border = BorderType.SimpleArrowRight;
			this.BorderColor = Color.Black;
			this.BorderWidth = 1;

			//this.BackColor = Color.FromArgb(153, 204, 1);
			this.BackColor = Color.FromArgb(160, 60, 160);
			this.BackColor2 = Color.FromArgb(250, 200, 250);
			this.BackLinearGradientMode = LinearGradientMode.ForwardDiagonal;
			this.BackBrushMode = BackgroundBrushMode.LinearGradient;

			//_format = new StringFormat(StringFormat.GenericDefault);
			//_format.Alignment = StringAlignment.Center;
			//_format.LineAlignment = StringAlignment.Center;

			this.EndUpdate();
		}
		#endregion

		#region Public Members

		#region Properties (Overrided)
		public override bool CanFocused
		{
			get
			{
				return false;
			}
		}
		public override bool CanMove
		{
			get
			{
				return false;
			}
		}
		public override bool CanResize
		{
			get
			{
				return false;
			}
		}
		public override bool CanSelect
		{
			get
			{
				return false;
			}
		}
		public override bool NoClip
		{
			get
			{
				return true;
			}
		}
		#endregion

		#region Methods (Overrided)
		public override void Dispose()
		{
			//dispose(_format);

			base.Dispose();
		}

		public override void PaintBackground(System.Windows.Forms.PaintEventArgs pe)
		{
			base.PaintBackground(pe);
		}
		public override void Paint(System.Windows.Forms.PaintEventArgs pe)
		{
			base.Paint(pe);
		}
		
		public override void ReadXml(System.Xml.XmlReader reader)
		{
			System.ComponentModel.BooleanConverter bc = new System.ComponentModel.BooleanConverter();
			while ( reader.Read() ) {
				switch ( reader.Name ) {
					case "DesignerControl":
						if ( reader.NodeType == System.Xml.XmlNodeType.EndElement )
							return;
						break;
					case "GenericFolder":
						if ( reader.MoveToAttribute("id") && reader.ReadAttributeValue() )
							this.RepositoryItem = new Repository.GenericFolder(int.Parse(reader.Value));
						if ( reader.MoveToAttribute("isRef") && reader.ReadAttributeValue() )
							this.IsReference = (bool)bc.ConvertFromString(reader.Value);
						break;
					default:
						ReadStandardProperties(reader);
						break;
				}
			}
		}
		public override void WriteXml(System.Xml.XmlWriter writer)
		{
			System.ComponentModel.BooleanConverter bc = new System.ComponentModel.BooleanConverter();
			WriteStandardProperties(writer);

			if ( _folder != null ) {
				writer.WriteStartElement("GenericFolder");
				writer.WriteAttributeString("id", _folder.ID.ToString());
				writer.WriteAttributeString("name", _folder.Name);
				writer.WriteAttributeString("isRef", bc.ConvertToString(this.IsReference));
				writer.WriteEndElement();
			}
		}
		#endregion

		#endregion

		#region Private Members

		#region Variables
		//private StringFormat _format;
		#endregion

		#endregion

		#region IRepositoryItemSymbol Members

		public bool SaveHierarchy
		{
			get { return true; }
		}

		private bool _isReference = false;
		public bool IsReference
		{
			get { return _isReference; }
			set { _isReference = value; }
		}

		public bool CanBeMainControl
		{
			get { return false; }
		}

		public OpenComposite.EII.Repository.IRepositoryItem RepositoryItem
		{
			get
			{
				return _folder;
			}
			set
			{
				_folder = value as Repository.GenericFolder;
				if ( _folder != null ) {
					this.Text = _folder.Name;
				}
			}
		}

		public void Load(int idRepository, int idDomain)
		{
		}
		public bool Save(int idRepository, int idDomain)
		{
			if ( _folder == null )
				return false;

			return _folder.Save();
		}

		public bool Delete(bool bAsk)
		{
			if ( _folder == null )
				return true;

			return _folder.Delete(true);
		}

		protected override void OnTextChanged(EventArgs e)
		{
			if ( _folder != null )
				_folder.Name = this.Text;
			base.OnTextChanged(e);
		}
		private Repository.GenericFolder _folder;


		#endregion
	}
}
